package com.ruoyi;

import com.ruoyi.framework.killbill.EventBusRegister;
import org.dromara.dynamictp.core.spring.EnableDynamicTp;
import org.killbill.bus.api.PersistentBus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ImportResource;
import org.springframework.scheduling.annotation.EnableAsync;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * 启动程序
 * 
 * @author ruoyi
 */
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
//@EnableDynamicTp
@ImportResource({"spring-killbill-queue.xml"})
@EnableAsync
public class RuoYiApplication
{
    private static final Logger log = LoggerFactory.getLogger(RuoYiApplication.class);

    /*@Resource(name = "dtpExecutor1")
    private ThreadPoolExecutor dtpExecutor1;*/

    public static void main(String[] args)
    {
        // System.setProperty("spring.devtools.restart.enabled", "false");
        SpringApplication.run(RuoYiApplication.class, args);
        System.out.println("(♥◠‿◠)ﾉﾞ  若依启动成功   ლ(´ڡ`ლ)ﾞ  \n" +
                " .-------.       ____     __        \n" +
                " |  _ _   \\      \\   \\   /  /    \n" +
                " | ( ' )  |       \\  _. /  '       \n" +
                " |(_ o _) /        _( )_ .'         \n" +
                " | (_,_).' __  ___(_ o _)'          \n" +
                " |  |\\ \\  |  ||   |(_,_)'         \n" +
                " |  | \\ `'   /|   `-'  /           \n" +
                " |  |  \\    /  \\      /           \n" +
                " ''-'   `'-'    `-..-'              ");
    }

    /*@Bean
    public CommandLineRunner init() {
        return (args) -> {
            dtpExecutor1.execute(() -> {
                System.out.println("dtpExecutor1 execute ... ...");
            });
        };
    }*/

    @Autowired
    PersistentBus eventBus;
    @Autowired
    private ApplicationContext applicationContext;


    @PostConstruct
    public void postConstruct() {
        //基于注解 自动注册到eventBus
        try {
            this.applicationContext.getBeansWithAnnotation(EventBusRegister.class).forEach((name, bean) -> {
                try {
                    log.info("bean={}",bean.getClass().getCanonicalName());
                    eventBus.register(bean);

                } catch (PersistentBus.EventBusException e) {
                    log.error(e.getMessage(),e);
                }
            });
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        }
        log.error("=========================================================");
    }
}
